﻿<!DOCTYPE HTML>
<html lang="zh">
<head>
<title>SubStr - 语法 &amp; 使用 | AutoHotkey v2</title>
<meta name="description" content="The SubStr function retrieves one or more characters from the specified position in a string." />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<link href="../static/theme.css" rel="stylesheet" type="text/css" />
<script src="../static/content.js" type="text/javascript"></script>
<script type="text/javascript">$(function(){0<=window.navigator.userAgent.toLowerCase().indexOf("ucbrowser")&&CaoNiMaDeUc()})</script>
</head>
<body>

<h1>SubStr</h1>

<p>Retrieves one or more characters from the specified position in a string.</p>

<pre class="Syntax">NewStr := <span class="func">SubStr</span>(String, StartingPos <span class="optional">, Length</span>)</pre>
<h2 id="Parameters">参数</h2>
<dl>

  <dt>String</dt>
  <dd>
    <p>类型: <a href="../Concepts.htm#strings">字符串</a></p>
    <p>The string whose content is copied. This may contain binary zero.</p>
  </dd>

  <dt>StartingPos</dt>
  <dd>
    <p>类型: <a href="../Concepts.htm#numbers">整数</a></p>
    <p>Specify 1 to start at the first character, 2 to start at the second, and so on (if <em>StartingPos</em> is 0 or beyond <em>String</em>'s length, an empty string is returned).</p>
    <p>Specify a negative <em>StartingPos</em> to start at that position from the right. 例如, -1 extracts the last character and -2 extracts the two last characters (but if <em>StartingPos</em> tries to go beyond the left end of the string, the extraction starts at the first character).</p>
  </dd>

  <dt>Length</dt>
  <dd>
    <p>类型: <a href="../Concepts.htm#numbers">整数</a></p>
    <p>If this parameter is omitted, it defaults to "all characters". Otherwise, specify the maximum number of characters to retrieve (fewer than the maximum are retrieved whenever the remaining part of the string is too short). You can also specify a negative <em>Length</em> to omit that many characters from the end of the returned string (an empty string is returned if all or too many characters are omitted).</p>
  </dd>

</dl>

<h2 id="Return_Value">返回值</h2>
<p>类型: <a href="../Concepts.htm#strings">字符串</a></p>
<p>This function returns the requested substring of the specified string.</p>

<h2 id="Related">相关</h2>
<p><a href="RegExMatch.htm">RegExMatch</a></p>

<h2 id="Examples">示例</h2>
<div class="ex" id="ExBasic">
<p><a href="#ExBasic">#1</a></p>
<pre>MsgBox SubStr("123abc789", 4, 3) <em>; Returns abc</em></pre>
</div>

<div class="ex" id="ExEnd">
<p><a href="#ExEnd">#2</a></p>
<pre>String := "The Quick Brown Fox Jumps Over the Lazy Dog"
MsgBox SubStr(String, 1, 19)  <em>; Returns "The Quick Brown Fox"</em>
MsgBox SubStr(String, -8)  <em>; Returns "Lazy Dog"</em>
</pre>
</div>

</body>
</html>